﻿Public Class WindowsMenu

    Public Sub WindowsToolStripMenuItem_DropDownOpened(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim wintoolitems = MainForm.WindowsToolStripMenuItem

        ' Clear up to the first separator
        For i As Integer = wintoolitems.DropDownItems.Count - 1 To 0 Step -1

            ' Is this the separator?
            Dim text = wintoolitems.DropDownItems(i).Text
            If text.Length > 0 Then
                If Char.IsDigit(text(0)) Then
                    wintoolitems.DropDownItems.RemoveAt(i)
                End If
            End If
        Next

        ' Calculate the window items for all the open tabs, etc
        For i As Integer = 0 To MainForm.EditorTabManager.TabCount - 1
            Dim tabpage As EditorTabPage = CType(MainForm.EditorTabManager.TabPages(i), EditorTabPage)
            Dim menuitemname As String =
                (MainForm.EditorTabManager.TabCount - i).ToString() & " " & tabpage.Text
            Dim menuitem =
                New ToolStripMenuItem(menuitemname, Nothing, AddressOf SelectWindow)
            wintoolitems.DropDownItems.Insert(0,
                    menuitem)
        Next

    End Sub

    Public Sub SelectWindow(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim menuitem = CType(sender, ToolStripMenuItem)
        For Each etab In MainForm.EditorTabManager.TabPages
            Dim editortab As EditorTabPage = CType(etab, EditorTabPage)
            Dim desiredtab As String = menuitem.Text.Substring(2, menuitem.Text.Count - 2)
            If editortab.Name = desiredtab Then
                MainForm.EditorTabManager.TabControl.SelectedTab = editortab
            End If
        Next
    End Sub

    Public Sub FocusEditor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not IsNothing(MainForm.EditorTabManager.CurrentTab) Then
            MainForm.EditorTabManager.CurrentTab.ScintillaEditor.Focus()
        End If
    End Sub

    Public Sub ShowClassBrowser(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not MainForm.Docks.ClassBrowserDock.Visible Then
            MainForm.Docks.ClassBrowserDock.Show(MainForm.DockPanel)
        Else
            MainForm.Docks.ClassBrowserDock.Hide()
        End If
    End Sub

    Public Sub ShowFileExplorer(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not MainForm.Docks.FileViewDock.Visible Then
            MainForm.Docks.FileViewDock.Show(MainForm.DockPanel)
        Else
            MainForm.Docks.FileViewDock.Hide()
        End If
    End Sub

    Public Sub ShowErrorView(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not MainForm.Docks.ErrorViewDock.Visible Then
            MainForm.Docks.ErrorViewDock.Show(MainForm.DockPanel)
        Else
            MainForm.Docks.ErrorViewDock.Hide()
        End If
    End Sub

    Public Sub ShowConsoleOutput(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not MainForm.Docks.ConsoleOutputDock.Visible Then
            MainForm.Docks.ConsoleOutputDock.Show(MainForm.DockPanel)
        Else
            MainForm.Docks.ConsoleOutputDock.Hide()
        End If
    End Sub

    Public Sub Close(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MainForm.EditorTabManager.CloseCurrentTab()
    End Sub

    Public Sub CloseOthers(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MainForm.EditorTabManager.CloseOthers(MainForm.EditorTabManager.CurrentTab)
    End Sub

    Public Sub CloseAll(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MainForm.EditorTabManager.CloseAll()
    End Sub
End Class
